package com.zhenlong01.zhentest.controller;

import com.google.zxing.WriterException;
import com.zhenlong01.zhentest.dao.BaoXiuManDao;
import com.zhenlong01.zhentest.dao.EquipmentDao;
import com.zhenlong01.zhentest.dao.EquipmentPicDao;
import com.zhenlong01.zhentest.entity.BaoXiuMan;
import com.zhenlong01.zhentest.entity.Equipment;
import com.zhenlong01.zhentest.entity.EquipmentPic;
import com.zhenlong01.zhentest.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.lang.Nullable;

/**
 * @author Administrator
 * @date 2019-08-29 8:06
 */
@RestController
public class EquipmentController {
    @Autowired
    EquipmentDao equipmentDao;

    @Autowired
    EquipmentPicDao equipmentPicDao;

    @Autowired
    BaoXiuManDao baoXiuManDao;

    @PostMapping("/addeq")
    @Transactional
    public Equipment addEq(@RequestBody  Equipment equipment) throws IOException, WriterException {
        System.out.println(equipment.getBaoXiuMan().getId());
        Equipment eq = equipmentDao.save(equipment);
        eq.setCreatedate(new Date());

        String count = (equipmentDao.count()+1)+"";

        int diff = 10 - count.length();
        String zero = "";
        for(int i=0;i<diff;i++){
            zero +="0";
        }
        //0000000001
        eq.seteNo(zero+count);
        eq.setBarCode(StringUtil.barCode(
                StringUtil.getWechatURL("http://47.110.59.63/baoxiu.html?eno="
                        +(zero+count+""))));



        for(EquipmentPic p:eq.getEquipmentPics()){
            p.setEquipment(eq);
            equipmentPicDao.save(p);
        }
        return eq;
    }

    @GetMapping("/findall_eq_by_baoxiuman")
    public Iterable<Equipment> findall_eq_by_baoxiuman(String openid,int p,int size){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        PageRequest pr =  PageRequest.of(p-1,size,sort);

        return equipmentDao.findAll(new Specification<Equipment>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Equipment> h, CriteriaQuery<?> q, CriteriaBuilder c) {
                List<Predicate> pList = new ArrayList<>();

                pList.add(c.equal(h.get("baoXiuMan").as(BaoXiuMan.class),
                        baoXiuManDao.findByOpenid(openid)));

                Predicate[] p2 = new Predicate[pList.size()];
                q.where(c.and(pList.toArray(p2)));
                return q.getRestriction();
            }
        }, pr);

    }

    @GetMapping("/findall_eq")
    public Page<Equipment> findall_eq(int p, int size){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
//
        PageRequest pr =  PageRequest.of(p-1,size,sort);
        // Pageable pageable = new PageRequest(p - 1, size);
        return equipmentDao.findAll(pr);
    }
}
